	.module ds1302.c
	.area text(rom, con, rel)
	.dbfile D:\01.Projects\JKElectronics\ATMEGA\CoreModule\sw\examples\lib\ds1302.c
	.dbfunc e bsp_ds1302_gpio_init _bsp_ds1302_gpio_init fV
	.even
_bsp_ds1302_gpio_init::
	.dbline -1
	.dbline 8
; 
; 
; #include "hw_config.h"
; #include "ds1302.h"
; 
; 
; void bsp_ds1302_gpio_init(void)		  
; {
	.dbline 10
; 	//  the clock terminal (RTC_CLK) data terminal (RTC_DATA) Chip select (RTC_CS) is set to output
; 	DS1302_DDR |=_BV(DS1302_SCLK_PIN_NO)|_BV(DS1302_IO_PIN_NO)|_BV(DS1302_RST_PIN_NO);
	in R24,0x17
	ori R24,112
	out 0x17,R24
	.dbline -2
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e ds1302_write _ds1302_write fV
;              i -> R20
;           data -> R10
;            add -> R12
	.even
_ds1302_write::
	xcall push_xgset303C
	mov R10,R18
	mov R12,R16
	.dbline -1
	.dbline 21
; }
; 
; /*******************************************
; Function name: DS1302_write
; Features: an address to write to the DS1302 a byte of data
; Parameters: REG - address value (register or RAM)
; data - to be written
; Return Value: None
; *******************************************/
; void ds1302_write(byte add,byte data) 
; { 
	.dbline 22
; 	byte i=0; 
	.dbline 24
; 	
; 	set_ds1302_io_ddr();     // configure the IO is the output
	sbi 0x17,5
	.dbline 25
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 26
; 	clr_ds1302_rst();        // clear reset, stop all operations
	cbi 0x18,6
	.dbline 27
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 28
; 	clr_ds1302_sclk();       // clear the clock, ready to operate
	cbi 0x18,4
	.dbline 29
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 30
; 	set_ds1302_rst();        // set reset to start operation
	sbi 0x18,6
	.dbline 31
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 33
; 	
; 	for(i=8;i>0;i--)         // This loop writes the control code
	ldi R20,8
	xjmp L8
L5:
	.dbline 34
; 	{ 
	.dbline 35
; 		if(add&0x01)
	sbrs R12,0
	rjmp L9
X0:
	.dbline 36
; 			set_ds1302_io();     // this bit is 1, set the data bits
	sbi 0x18,5
	xjmp L10
L9:
	.dbline 38
; 		else
; 		  	clr_ds1302_io();     // this bit is 0, clear data bits
	cbi 0x18,5
L10:
	.dbline 40
; 		  	
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 41
; 		set_ds1302_sclk();     // generates a clock pulse, the write data
	sbi 0x18,4
	.dbline 42
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 43
; 		clr_ds1302_sclk(); 
	cbi 0x18,4
	.dbline 44
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 45
; 		add>>=1;         // shift, ready to write the next one
	lsr R12
	.dbline 46
; 	} 
L6:
	.dbline 33
	dec R20
L8:
	.dbline 33
	ldi R24,0
	cp R24,R20
	brlo L5
X1:
	.dbline 47
; 	for(i=8;i>0;i--)      // write code for this recycling
	ldi R20,8
	xjmp L14
L11:
	.dbline 48
; 	{ 
	.dbline 49
; 		if(data&0x01)
	sbrs R10,0
	rjmp L15
X2:
	.dbline 50
; 			set_ds1302_io(); 
	sbi 0x18,5
	xjmp L16
L15:
	.dbline 52
; 		else
; 	  		clr_ds1302_io(); 
	cbi 0x18,5
L16:
	.dbline 54
; 		
; 	  	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 55
; 		set_ds1302_sclk(); 
	sbi 0x18,4
	.dbline 56
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 57
; 		clr_ds1302_sclk(); 
	cbi 0x18,4
	.dbline 58
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 59
; 		data>>=1; 
	lsr R10
	.dbline 60
; 	} 
L12:
	.dbline 47
	dec R20
L14:
	.dbline 47
	ldi R24,0
	cp R24,R20
	brlo L11
X3:
	.dbline 62
; 	
; 	clr_ds1302_rst(); 
	cbi 0x18,6
	.dbline 63
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 64
; 	clr_ds1302_io_ddr();      // clear output state
	cbi 0x17,5
	.dbline 65
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline -2
L4:
	.dbline 0 ; func end
	xjmp pop_xgset303C
	.dbsym r i 20 c
	.dbsym r data 10 c
	.dbsym r add 12 c
	.dbend
	.dbfunc e ds1302_read _ds1302_read fc
;           data -> R20
;              i -> R22
;            add -> R10
	.even
_ds1302_read::
	xcall push_xgsetF00C
	mov R10,R16
	.dbline -1
	.dbline 75
; }
; 
; /*******************************************
; Function name: DS1302_read
; Features: an address from the DS1302 to read one byte of data
; Parameters: add - address value (register or RAM)
; Return Value: data - the data read out
; ********************************************/
; byte ds1302_read(byte add)
; { 
	.dbline 76
; 	byte i=0,data=0; 
	.dbline 76
	clr R20
	.dbline 78
; 
; 	add+=1;                  // read flag
	inc R10
	.dbline 79
; 	set_ds1302_io_ddr();     // output ports
	sbi 0x17,5
	.dbline 80
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 81
; 	clr_ds1302_rst();        // clear reset
	cbi 0x18,6
	.dbline 82
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 83
; 	clr_ds1302_sclk();       // clear clock
	cbi 0x18,4
	.dbline 84
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 85
; 	set_ds1302_rst();        // Set Reset
	sbi 0x18,6
	.dbline 86
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 88
; 	
; 	for(i=8;i>0;i--)         // This loop writes the address code
	ldi R22,8
	xjmp L21
L18:
	.dbline 89
; 	{ 
	.dbline 90
; 		if(add&0x01)
	sbrs R10,0
	rjmp L22
X4:
	.dbline 91
; 	 	{
	.dbline 92
; 	  		set_ds1302_io();
	sbi 0x18,5
	.dbline 93
; 	 	} 
	xjmp L23
L22:
	.dbline 95
; 		else
; 	 	{
	.dbline 96
; 	  	clr_ds1302_io();
	cbi 0x18,5
	.dbline 97
; 	 	}
L23:
	.dbline 99
; 
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 100
; 		set_ds1302_sclk(); 
	sbi 0x18,4
	.dbline 101
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 102
; 		clr_ds1302_sclk(); 
	cbi 0x18,4
	.dbline 103
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 104
; 		add>>=1; 
	lsr R10
	.dbline 105
; 	} 
L19:
	.dbline 88
	dec R22
L21:
	.dbline 88
	ldi R24,0
	cp R24,R22
	brlo L18
X5:
	.dbline 106
; 	clr_ds1302_io_ddr();      // input port
	cbi 0x17,5
	.dbline 107
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 108
; 	for(i=8;i>0;i--)         // This loop reads data from 1302
	ldi R22,8
	xjmp L27
L24:
	.dbline 109
; 	{ 
	.dbline 110
; 		data>>=1; 
	lsr R20
	.dbline 111
; 		if(in_ds1302_io())
	sbis 0x16,5
	rjmp L28
X6:
	.dbline 112
; 	 	{
	.dbline 113
; 	   		data|=0x80;
	ori R20,128
	.dbline 114
; 	 	}
L28:
	.dbline 115
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 116
; 		set_ds1302_sclk(); 
	sbi 0x18,4
	.dbline 117
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 118
; 		clr_ds1302_sclk(); 
	cbi 0x18,4
	.dbline 119
; 		delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 120
; 	} 
L25:
	.dbline 108
	dec R22
L27:
	.dbline 108
	ldi R24,0
	cp R24,R22
	brlo L24
X7:
	.dbline 122
; 	
; 	clr_ds1302_rst(); 
	cbi 0x18,6
	.dbline 123
; 	delay_us(2); 
	ldi R16,2
	ldi R17,0
	xcall _delay_us
	.dbline 125
; 	
; 	return(data); 
	mov R16,R20
	.dbline -2
L17:
	.dbline 0 ; func end
	xjmp pop_xgsetF00C
	.dbsym r data 20 c
	.dbsym r i 22 c
	.dbsym r add 10 c
	.dbend
	.dbfunc e check_ds1302 _check_ds1302 fc
	.even
_check_ds1302::
	.dbline -1
	.dbline 136
; 	
; } 
; 
; /*******************************************
; Function name: check_DS1302
; Function: detect DS1302 is working
; Parameters: None
; Return Value: exist - to TRUE for the detection of the DS1302, as not detected to FALSE
; ********************************************/
; byte check_ds1302(void) 
; { 
	.dbline 137
; 	ds1302_write(ds1302_control_add,0x80); 
	ldi R18,128
	ldi R16,142
	xcall _ds1302_write
	.dbline 139
; 	
; 	if(ds1302_read(ds1302_control_add)==0x80)
	ldi R16,142
	xcall _ds1302_read
	cpi R16,128
	brne L31
X8:
	.dbline 140
; 		return 1; 
	ldi R16,1
	xjmp L30
L31:
	.dbline 142
; 	
; 	return 0; 
	clr R16
	.dbline -2
L30:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e ds1302_set_time _ds1302_set_time fV
;           addr -> R20
;              i -> R22
;       set_time -> R10,R11
	.even
_ds1302_set_time::
	xcall push_xgsetF00C
	movw R10,R16
	.dbline -1
	.dbline 152
; } 
; 
; /*******************************************
; Function name: DS1302_setT
; Function: Set the DS1302 time
; Parameters: ptTimeD - an array of pointers to set the time
; Return Value: None
; ********************************************/
; void ds1302_set_time(byte set_time[])
; {
	.dbline 154
; 	byte i;
; 	byte addr = 0x80; 		 //  seconds register write address from the beginning
	ldi R20,128
	.dbline 156
; 	
; 	ds1302_write(ds1302_control_add|0x00,0x00); // control commands, allowing a write operation
	clr R18
	ldi R16,142
	xcall _ds1302_write
	.dbline 157
; 	delay_us(5000); 
	ldi R16,5000
	ldi R17,19
	xcall _delay_us
	.dbline 159
; 	
; 	for(i=0;i<7;i++)
	clr R22
	xjmp L37
L34:
	.dbline 160
; 	{
	.dbline 161
; 		ds1302_write(addr|0x00,set_time[i]);   // second week of sun and the moon in time-sharing
	mov R30,R22
	clr R31
	add R30,R10
	adc R31,R11
	ldd R18,z+0
	mov R16,R20
	xcall _ds1302_write
	.dbline 162
; 		addr+=2;
	subi R20,254    ; addi 2
	.dbline 163
; 		delay_us(1000); 
	ldi R16,1000
	ldi R17,3
	xcall _delay_us
	.dbline 164
; 	}
L35:
	.dbline 159
	inc R22
L37:
	.dbline 159
	cpi R22,7
	brlo L34
X9:
	.dbline 165
; 	ds1302_write(ds1302_control_add|0x00,0x80); 		// control commands, WP bit is 1, does not allow write operation
	ldi R18,128
	ldi R16,142
	xcall _ds1302_write
	.dbline -2
L33:
	.dbline 0 ; func end
	xjmp pop_xgsetF00C
	.dbsym r addr 20 c
	.dbsym r i 22 c
	.dbsym r set_time 10 pc
	.dbend
	.dbfunc e ds1302_read_time _ds1302_read_time fV
;       set_time -> R20,R21
	.even
_ds1302_read_time::
	xcall push_xgset300C
	movw R20,R16
	.dbline -1
	.dbline 198
; }
; 
; #if 0
; /*******************************************
; Function name: ds1302_write_time
; Function: Clock data write to 1302
; Parameters: None
; Return Value: None
; ********************************************/
; void ds1302_write_time(void)
; {  
; 	ds1302_write(ds1302_control_add, 0x00); // close the write protection
; 	ds1302_write(ds1302_sec_add, 0x80); // pause
; 	ds1302_write(ds1302_charger_add, 0x90); // trickle charge
; 	ds1302_write(ds1302_year_add, ds1302_time[0]); // years
; 	ds1302_write(ds1302_month_add, ds1302_time[1]); // month
; 	ds1302_write(ds1302_date_add, ds1302_time[2]); // day
; 	ds1302_write(ds1302_day_add, ds1302_time[6]); // weeks
; 	ds1302_write(ds1302_hr_add, ds1302_time[3]); // time
; 	ds1302_write(ds1302_min_add, ds1302_time[4]); // min
; 	ds1302_write(ds1302_sec_add, ds1302_time[5]); // sec
; 	ds1302_write(ds1302_control_add, 0x80); // open write protection 
; }
; #endif
; 
; /*******************************************
; Function name: ds1302_read_time
; Function: read the DS1302's current time
; Parameters:
; Return Value: None
; ********************************************/
; void ds1302_read_time (byte set_time[])
; {
	.dbline 199
; 	set_time[0] = ds1302_read(ds1302_year_add);  // years
	ldi R16,140
	xcall _ds1302_read
	movw R30,R20
	std z+0,R16
	.dbline 200
; 	set_time[1] = ds1302_read(ds1302_month_add); // month
	ldi R16,136
	xcall _ds1302_read
	movw R30,R20
	std z+1,R16
	.dbline 201
; 	set_time[2] = ds1302_read(ds1302_date_add); // day
	ldi R16,134
	xcall _ds1302_read
	mov R10,R16
	movw R30,R20
	std z+2,R10
	.dbline 202
; 	set_time[6] = ds1302_read(ds1302_day_add); // weeks
	ldi R16,138
	xcall _ds1302_read
	mov R10,R16
	movw R30,R20
	std z+6,R10
	.dbline 203
; 	set_time[3] = ds1302_read(ds1302_hr_add); // time
	ldi R16,132
	xcall _ds1302_read
	mov R10,R16
	movw R30,R20
	std z+3,R10
	.dbline 204
; 	set_time[4] = ds1302_read(ds1302_min_add); // min
	ldi R16,130
	xcall _ds1302_read
	mov R10,R16
	movw R30,R20
	std z+4,R10
	.dbline 205
; 	set_time[5] = ds1302_read(ds1302_sec_add); // sec
	ldi R16,128
	xcall _ds1302_read
	mov R10,R16
	movw R30,R20
	std z+5,R10
	.dbline -2
L38:
	.dbline 0 ; func end
	xjmp pop_xgset300C
	.dbsym r set_time 20 pc
	.dbend
; }
; 
; 
; 
; #if 0
; /*******************************************
; Function name: ds1302_display_time
; Feature: 12864 shows the current time (line 1 format: year - month - day week; 2 line format: time - minutes - seconds)
; Parameters: time [] - array of time
; Return Value: None
; ********************************************/
; void ds1302_display_time(byte set_time[])
; {
; 	byte asc[2];
; 	byte line1[11] = {0,0 ,'-', 0,0 ,'-', 0,0, '', 0, '\ 0'}; // display an array of characters on line 1
; 	byte line2[9] = {0,0 ,':', 0,0 ,':', 0,0, '\ 0'};  // display an array of characters in line 2
; 
; 	bcd2ascii (time[3], asc); // time
; 	line2[0] = asc[0];
; 	line2[1] = asc[1];
; 	bcd2ascii (time[4], asc); // min
; 	line2[3] = asc[0];
; 	line2[4] = asc[1];
; 	bcd2ascii (time[5], asc); // sec
; 	line2[6] = asc[0];
; 	line2[7] = asc[1];
; 
; 	bcd2ascii (time[0], asc); // for the assignment in line 1
; 	line1[0] = asc[0];
; 	line1[1] = asc[1];
; 	bcd2ascii (time[1], asc); // for the assignment on line 1
; 	line1[3] = asc[0];
; 	line1[4] = asc[1];
; 	bcd2ascii (time[2], asc); // for the assignment on line 1
; 	line1[6] = asc[0];
; 	line1[7] = asc[1];
; 	bcd2ascii (time[6], asc); // line 1 week for the first assignment
; 	line1[9] = asc[1];
; 
; #if 0
; 	Char_Set_XY (1,0, "20"); // line 1 position from the 1st show, will appear as 2007 2007 in the form of
; 	Char_Set_XY (2,0, line1);
; 	Char_Set_XY (2,1, line2); // line 2 from the first two position display * /
; #endif	
; }
; #endif
; 
; 
